package situ.autoweter.controller;



import org.apache.ibatis.annotations.Param;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.javafx.collections.MappingChange;
import com.sun.org.apache.bcel.internal.generic.RETURN;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import situ.autoweter.entity.*;
import situ.autoweter.service.RdChangemaxvalueService;
import situ.autoweter.service.RdChangevalueService;



import situ.autoweter.service.RdReadService;
import situ.autoweter.service.SyAreaService;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
/**
 * RdRead的路由接口服务
 *
 * @author 胡文
 *
 */
@Controller
public class RdReadController {

    /** RdReadService服务 */
    @Autowired
    private RdReadService rdReadService;
    @Autowired
    private SyAreaService syAreaService;





    /**
     * 查询所有RdRead数据的方法
     * @param value
     * @return
     */

    @RequestMapping(value ="queryRdRead", produces = {"application/json;charset=UTF-8"})
    public String find(RdRead value,Map map) {
        List<RdRead> list= rdReadService.find(value);

        map.put("items",list);
        return "页面名称自己打";
    }

    @RequestMapping(value = "queryRdRead2", produces = {"application/json;charset=UTF-8"})
    public String find2(RdRead value,Map map) {
        List<SyArea> areas=syAreaService.find_onetomany();
        map.put("areas", areas);
        List<Map> list= rdReadService.find2(value);
        map.put("items",list);
        return "page/rd_enter.html";
    }

    /**
     * 通过id查询RdRead数据的方法
     *
     * @return
     */
    @RequestMapping(value = "queryRdReadById/{id}", produces = {"application/json;charset=UTF-8"})
    public String findOne(@PathVariable(name="id") Object id,Map map) {
        RdRead obj=  rdReadService.findOne(id);
        map.put("RdRead",obj);
        return "页面名称自己改";
    }

    /**
     * 罗艳写id查
     * */
    @RequestMapping(value = "queryupdate/{id}", produces = {"application/json;charset=UTF-8"})
    public String queryupdate(@PathVariable(name="id") Integer id,Map map) {
        Map obj=  rdReadService.
                queryupdate(id);
        map.put("RdRead",obj);
        return "page/rd_enter_setMaxValue.html";
    }
    /**
     * 罗艳写的修改
     */
    @RequestMapping(value = "updatese", produces = {"application/json;charset=UTF-8"})
    public String sexMaxValueUpdate(RdChangemaxvalue rdChangevalue,RdRead value,@RequestParam(value = "max")String max) {
        int m=Integer.parseInt(max);
//        System.out.println(m+"hh"+max);
        value.setMaxValue(m);
        rdReadService.updateNotNullById(value);
        this.addMaxValue(rdChangevalue,max);
        return "redirect:/queryRdRead2";
    }
    @Autowired
    RdChangemaxvalueService rdChangemaxvalueService;
    public void addMaxValue(RdChangemaxvalue value, @RequestParam(value = "max")String max){
        int m=Integer.parseInt(max);
        Date date=new Date();
        value.setID(null);
        value.setDatee( date.toInstant());
        value.setEmpID(value.getEmpID());
        value.setUserNo(value.getUserNo());
        value.setMeterNo(value.getMeterNo());//水表编码
        value.setMaxValue1(value.getMaxValue1());//修正前最大表码值
        value.setMaxValue2(m);//修正后最大表码值
        value.setRemark(value.getRemark());
        rdChangemaxvalueService.saveNotNull(value);
    }

    /**
     * 插入RdRead属性不为空的数据方法
     *
     * @return
     */
    @RequestMapping(value = "saveRdRead", produces = {"application/json;charset=UTF-8"})
    public String save(RdRead value) {
        rdReadService.saveNotNull(value);

        return "redirect:/queryRdRead";

    }


	
	/**
	 * 更新RdRead属性不为空的数据方法
	 * 
	 * @return
	 */
	@RequestMapping(value = "updateRdRead", produces = {"application/json;charset=UTF-8"})
	public String update(RdRead value) {
		  rdReadService.updateNotNullById(value);
          return "redirect:/queryRdRead";
	}
	
	/**
	 * 通过id删除RdRead数据方法
	 * 
	 * @return
	 */
	@RequestMapping(value = "delRdReadById/{id}", produces = {"application/json;charset=UTF-8"})
	public String delete(@PathVariable(name="id") Object id) {
		  rdReadService.deleteById(id);
          return "redirect:/queryRdRead";
	}

	/**
	 * 侯慧林写的修改
	 */
	@RequestMapping(value = "updateRdReadCBR", produces = {"application/json;charset=UTF-8"})
	public String updateRdReadCBR(@RequestParam("ReadEmpID") Integer ReadEmpID, @RequestParam("id")Integer id) {
		rdReadService.updateNotNullByIdHHl(ReadEmpID,id);
		return "redirect:/rd_task";


    }



    @RequestMapping(value ="/rd_reportZero.html")
    public String queryRead(RdRead rdRead,Map map,SyArea svalue,
                            @RequestParam(value ="date1",required = false)String date1,
                            @RequestParam(value = "date2",required = false)String date2,
                            @RequestParam(defaultValue ="1",required = false) int startPage,
                            @RequestParam(defaultValue = "5",required = false) int PageSize
    ) {


        PageHelper.startPage(startPage, PageSize);
        if(date1!=null && date1!=""){
            rdRead.setReadYear(Integer.parseInt(date1.split("-")[0]));
            rdRead.setReadMonth(Integer.parseInt(date1.split("-")[1]));
        }
        if(date2!=null && date2!=""){
            rdRead.setReadYear2(Integer.parseInt(date2.split("-")[0]));
            rdRead.setReadMonth2(Integer.parseInt(date2.split("-")[1]));
        }
        if((date1!=null && date1!="")&&(date2!=null && date2!="")){
            //当前时间输入框中的时间在后时间输入框的时间之后时
            if(rdRead.getReadYear()>rdRead.getReadYear2()){
                int a= rdRead.getReadYear();
                rdRead.setReadYear(rdRead.getReadYear2());
                rdRead.setReadYear2(a);
                int b=rdRead.getReadMonth();
                rdRead.setReadMonth(rdRead.getReadMonth2());
                rdRead.setReadMonth2(b);
                String d=date1;
                date1=date2;
                date2=d;

            }

        }


        List<RdRead> list;
        if(rdRead.getVolumeId()!=null
                || rdRead.getReadYear()!=null
                || rdRead.getReadMonth()!=null
                || rdRead.getAudit()!=null){

            list=rdReadService.selectByif(rdRead);
        }else{
            list= rdReadService.find(rdRead);
        }
        List<SyArea> areas=syAreaService.find_onetomany();
        PageInfo pageInfo = new PageInfo<RdRead>(list,5);
        map.put("pageInfo", pageInfo);
        map.put("startPage", startPage);
        map.put("PageSize", PageSize);

//抄表数据
        map.put("items",list);
        //辖区
        map.put("areas", areas);
        //搜索的时间
        map.put("date1",date1);
        map.put("date2", date2);
        return "page/rd_reportZero.html";
    }
    /**
     * ly id查上月表码数
     */
    @RequestMapping(value = "queryChangeValueById/{id}", produces = {"application/json;charset=UTF-8"})
    public String queryChangeValueById(@PathVariable(name="id") Integer id,Map map){
        Map obj=rdReadService.queryChangeValueById(id);
        map.put("ChangeValue", obj);
        Date date=new Date();
        map.put("date", date);
        return "page/rd_enter_setPreValue.html";
    }

    /**
     * 上月修改表码数  int m=Integer.parseInt(max);,@RequestParam(value ="max")String max
     *         value.setMaxValue(m);
     */

    @RequestMapping(value = "updateChangeValue",produces = {"application/json;charset=UTF-8"})
    public String updateChangeValue(RdChangevalue rdChangevalue,RdRead value,@RequestParam(value = "max")String max){
        int m=Integer.parseInt(max);
        value.setPreValue(m);
        rdReadService.updateNotNullById(value);
        this.addChangeValue(rdChangevalue, max);
        return  "redirect:/queryRdRead2";
    }
@Autowired
    RdChangevalueService rdChangevalueService;
    public void addChangeValue(RdChangevalue value,@PathVariable(value = "max")String max){
        int m=Integer.parseInt(max);
        Date date=new Date();
        value.setID(null);
        value.setUserNo(value.getUserNo());
        value.setDatee(date.toInstant());
        value.setEmpID(value.getEmpID());
        value.setPreValue1(value.getPreValue1());
        value.setPreValue2(m);
        value.setRemark(value.getRemark());
        rdChangevalueService.saveNotNull(value);

    }
/**
 * 抄表情况查询的分页
 */
    @RequestMapping(value = "queryReportReadingFY",produces = {"application/json;charset=UTF-8"})
    public String queryReportReadingFY(
            RdRead rdRead,
            @RequestParam(defaultValue = "1",required = false) int startPage,
            @RequestParam(defaultValue = "5",required = false) int pagesize,
            Map map){
        PageHelper.startPage(startPage,pagesize);
        List<Map> maps=rdReadService.queryReportReadingFY(rdRead);
        PageInfo pageInfo=new PageInfo<Map>(maps,5);
        map.put("pageInfo", pageInfo);
        map.put("startPage", startPage);
        map.put("pagesize", pagesize);
        map.put("maps", maps);

        return "page/rd_reportReading.html";
    }
    /**
     * 跳首页
     */
    @RequestMapping("workspace")
    public String workspace(){
        return "workspace.html";
    }




//刘旻的东西
@RequestMapping(value = "ShowUs", produces = {"application/json;charset=UTF-8"})
public String ShowUs(Map map,RdRead rdRead){
    List<Map> list=rdReadService.ShowUS(rdRead);
    map.put("list", list);
    return "page/rd_auditUser.html";
}
    @RequestMapping(value = "MhFind")
    public String MhFind(USUser1 user, Map map){
        List<Map> list=rdReadService.MhFind(user);
        map.put("list", list);
        return "page/rd_auditUser.html";
    }
    @RequestMapping(value="Update/{ID}/{VolumeId}")
    public String Update(Map map,@PathVariable(value = "ID",required = false) int ID,
                         @PathVariable(value = "ID",required = false) int VolumeId){
        rdReadService.Update(ID);
        return queryById(VolumeId,map);
    }

    @RequestMapping(value="Update1/{ID}")
    public String Update(Map map, @PathVariable(value = "ID",required = false) Integer ID, HttpServletRequest request){
        rdReadService.Update(ID);
        return this.ShowUs(map,new RdRead());
    }

    @RequestMapping(value = "Maniy")
    public String Many(Map map,@RequestParam(value = "ID") Integer[] ID){
        for(Integer i:ID){
            rdReadService.Update(i);
        }

        return this.ShowUs(map,new RdRead());

    }
    @RequestMapping(value = "Many1")
    public String Many1(Map map,@RequestParam(value = "ID") Integer[] ID){
        for(Integer i:ID){
            rdReadService.Update(i);
        }
        return this.Show(map);

    }
    @RequestMapping(value = "rd_audit", produces = {"application/json;charset=UTF-8"})
    public String Show(Map map){

        List<Map> list=rdReadService.Show();
        map.put("list", list);
        return "page/rd_audit.html";
    }

    @RequestMapping(value = "queryById1/{VolumeId}", produces = {"application/json;charset=UTF-8"})
    public String queryById(@PathVariable("VolumeId") int VolumeId,Map map){
        List<Map> list=rdReadService.queryById(VolumeId);
        map.put("list",list);
        map.put("VolumeId", VolumeId);
        return "page/rd_audit_details.html";
    }
}
